<?php
namespace app\admin\controller;
use app\model\Banner;
use app\model\Config;
use app\model\Notice;
use think\Cache;
use app\model\Config as SystemConfig;
use app\model\SystemLog;
use oss\QiniuCloud;

class SystemController extends AuthController
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {

        return $this->fetch();
    }

    public function clearCache()
    {
        Cache::clear() ? $this->success('缓存清理成功') : $this->error('清理失败');
    }

    /**
     * 轮播图
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function banner()
    {
        $result = Banner::all(['status' => '1']);
        $this->assign('list', $result);
        return $this->fetch();
    }

    /**
     * 轮播图添加
     * @return mixed
     */
    public function bannerAdd()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            //图片上传
            $file = $this->request->file("pic");
            if (empty($file)) {
                $this->error('请上传图片');
            }
            $path = ROOT_PATH . 'public' . DS . 'upload'. DS. 'banner'. DS ;
            $pic = $file->validate(['ext' => 'jpg,png,jpeg'])->move($path);
            $pic_name = $pic->getFilename();
            $pic_path = $pic->getSaveName();
            $pic_url = QiniuCloud::upload($pic_name,$path.$pic_path);

            $data['pic'] = $pic_url;

            $banner = new Banner();
            $banner->data($data);
            $banner->save();
            $this->success('添加成功','admin/System/banner');
        } else {
            return $this->fetch();
        }
    }

    /**
     * 轮播图删除
     * @return array
     */
    public function bannerDel()
    {
        $id = $this->request->param('id');
        Banner::destroy(['id' => $id]);
        $this->success('轮播图删除成功','admin/System/banner');
    }

    /**
     * 公告栏
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function notice()
    {
        $result = Notice::all(['status' => '1']);
        $this->assign('list', $result);
        return $this->fetch();

    }

    /**
     * 公告添加
     * @return mixed
     */
    public function noticeAdd()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $notice = new Notice();
            $notice->data($data);
            $notice->save();
            $this->success('添加成功','admin/System/notice');
        } else {
            return $this->fetch();
        }
    }

    /**
     * 删除公告栏
     * @return array
     */
    public function noticeDel()
    {
        $id = $this->request->param('id');
        Notice::destroy(['id' => $id]);
        $this->success('公告删除成功','admin/System/notice');
    }

    /**
     * 工资提成
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function salary()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $value = $this->request->post('value');
            return SystemConfig::where('id', $id)->update(['value' => $value]) ? msgReturn(true, '修改成功', null) : msgReturn(false, 'error', null);
        } else {
            $result = SystemConfig::all(['name' => 'salary']);
            $this->assign('salary', $result);
            return $this->fetch();
        }
    }

    /**
     * 系统日志
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function log()
    {
        $model = new SystemLog();
        $result = $model->order('create_time desc')->paginate(20);

        $this->assign('list', $result);
        return $this->fetch();
    }

}